# -*- coding: utf-8 -*-
'''
@文件    :sms.py
@说明    :短信服务(short message service)
@时间    :2024/12/12 14:18:31
@作者    :浪晋
@版本    :1.0
'''

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20210111 import sms_client, models
from typing import List
import json


class TencentSMS:
    def __init__(self,SecretId: str, SecretKey: str, SmsSdkAppId: str, SignName: str):
        self.SecretId = SecretId
        self.SecretKey = SecretKey
        self.SmsSdkAppId = SmsSdkAppId
        self.SignName = SignName
                
    def get_client(self):
        cred = credential.Credential(self.SecretId, self.SecretKey)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "sms.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        region = "ap-beijing"
        client = sms_client.SmsClient(cred, region, clientProfile)
        return client
    
    def send_sms(
            self, 
            TemplateId: str,
            PhoneNumberSet: List = [],
            TemplateParamSet: List = []
        ):
        """
        发送短信
        响应格式:{"SendStatusSet": [{"SerialNo": "3363:140938803417349340998207606", "PhoneNumber": "+8618202876063", "Fee": 1, "SessionContext": "", "Code": "Ok", "Message": "send success", "IsoCode": "CN"}], "RequestId": "c51c04ac-8af2-44aa-8d28-72309aa7e6af"}
        """
        client = self.get_client()
        req = models.SendSmsRequest()
        params = {
            "SmsSdkAppId": self.SmsSdkAppId,
            "SignName": self.SignName,
            "TemplateId": TemplateId,
            "PhoneNumberSet": PhoneNumberSet,
            "TemplateParamSet": TemplateParamSet
        }
        req.from_json_string(json.dumps(params))
        response = client.SendSms(req)
        return json.loads(response.to_json_string())

